package com.obixlabs.security.credentialsstore.model;

import java.io.Serializable;


public class ObixWebSSOSession implements Serializable
{
	private static final long serialVersionUID = -8331023841558362238L;
	
	private User user;
	private String sessionId;
	private long lastTouched;
	
	private ObixWebSSOSession() {setLastTouched(System.currentTimeMillis());}
	
	public ObixWebSSOSession(User user, String sessionId)
	{
		this();
		setUser(user);
		setSessionId(sessionId);
	}
	
	public User getUser(){return user;}
	public void setUser(User user){this.user = user;}
	
	public String getSessionId(){return sessionId;}
	private void setSessionId(String sessionId)
	{this.sessionId = sessionId;}
	
	public long getLastTouched(){return lastTouched;}
	private void setLastTouched(long lastTouched)
	{this.lastTouched = lastTouched;}
		
	public void touch() {setLastTouched(System.currentTimeMillis());}
	
	public boolean isValid(long timeout) 
	{return timePeriodElapsed(timeout);}
	
	public boolean isRefreshDue(long refreshInterval) 
	{
		if (refreshInterval<=0)
			return false;		
		else return !timePeriodElapsed(refreshInterval);
	}

	
	
	private boolean timePeriodElapsed(long timeout)
	{
		boolean result;
		
		long currentTime = System.currentTimeMillis();
		long timeElapsedSinceLastTouched = currentTime - lastTouched;
		if (timeElapsedSinceLastTouched>=timeout)
			result = false;
		else result = true;		
		return result;		
	}	
}//end class def